package com.kantboot.functional.email.setting;

import cn.hutool.extra.mail.MailAccount;
import com.kantboot.system.setting.service.ISysSettingService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * 功能邮箱配置
 * 用于获取邮箱账号配置
 */
@Component
public class FunctionalEmailSetting {

    @Resource
    private ISysSettingService settingService;

    /**
     * 获取邮箱账号配置
     * MailAccount是Hutool封装的邮箱账号配置类，用于发送邮件
     * @return 邮箱账号配置
     */
    public MailAccount getMailAccount() {
        // start: 设置邮箱端口
        String portStr = settingService.getValue("functionalEmailSmtpPort");
        int port = 25;
        if (portStr != null) {
            port = Integer.parseInt(portStr);
        }
        // end: 设置邮箱端口

        MailAccount account = new MailAccount();
        account.setHost(settingService.getValue("functionalEmailSmtpHost"));
        account.setPort(port);
        account.setAuth(true);
        account.setSslEnable(true);
        account.setFrom(settingService.getValue("functionalEmailSmtpEmail"));
        account.setUser(settingService.getValue("functionalEmailSmtpUsername"));
        account.setPass(settingService.getValue("functionalEmailSmtpPassword"));
        return account;
    }

}
